package com.model.dao;
// default package
// Generated Feb 26, 2014 2:36:33 PM by Hibernate Tools 3.4.0.CR1

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 * Home object for domain model class RoleTable.
 * @see .RoleTable
 * @author Hibernate Tools
 */
@Stateless
public class RoleTableDAO {

	private static final Log log = LogFactory.getLog(RoleTableDAO.class);

	Session session;
	Transaction tx;


	public void persist(RoleTable transientInstance) {
		log.debug("persisting RoleTable instance");
		try {
			session = SessionFactoryUtil.getSessionFactory()
					.getCurrentSession();
			tx = session.beginTransaction();
			session.persist(transientInstance);
			tx.commit();
			log.debug("persist successful");
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
	}

	public void remove(RoleTable persistentInstance) {
		log.debug("removing RoleTable instance");
		try {
			session = SessionFactoryUtil.getSessionFactory()
					.getCurrentSession();
			tx = session.beginTransaction();
			session.delete(persistentInstance);
			tx.commit();
			log.debug("remove successful");
		} catch (RuntimeException re) {
			log.error("remove failed", re);
			throw re;
		}
	}

	public RoleTable merge(RoleTable detachedInstance) {
		log.debug("merging RoleTable instance");
		try {
			session = SessionFactoryUtil.getSessionFactory()
					.getCurrentSession();
			tx = session.beginTransaction();
			RoleTable result = (RoleTable) session
					.merge(detachedInstance);
			tx.commit();
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public RoleTable findById(int id) {
		log.debug("getting RoleTable instance with id: " + id);
		try {
			session = SessionFactoryUtil.getSessionFactory()
					.getCurrentSession();
			tx = session.beginTransaction();
			RoleTable instance = (RoleTable) session.get(
					RoleTable.class, id);
			tx.commit();
			log.debug("get successful");
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<RoleTable> findAll() {
		log.debug("getting RoleTable instance ");
		try {
			session = SessionFactoryUtil.getSessionFactory()
					.getCurrentSession();
			tx = session.beginTransaction();
			//Query query = session.createQuery("select distinct ot from OrderTable ot Join fetch ot.orderItemsTables oit  join fetch oit.childcatalogTable cct join fetch cct.mastercatalogTable where orderstatus <> 'Delivered'");
			Query query = session.createQuery("select distinct role  from RoleTable role left Join fetch role.rolePermissionTables pt left Join fetch pt.permissionTable where role.active= 'Y'");
			List<RoleTable> list = query.list();
			tx.commit();
			log.debug("get successful");
			return list;
		} catch (RuntimeException re) {
			System.out.println(re);
			session.close();
			if (tx != null) {
				tx.rollback();
			}
			log.error("get failed", re);
			throw re;
		}
	}
	public static void main(String[] args) throws Exception {
		RoleTableDAO dao=new RoleTableDAO();
		System.out.println(dao.findAll().size());
	}
}
